package com.A21MobileSales;

import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import android.content.Context;

import com.A21.Object.A21OrderHeader;
import com.A21.Object.Global;
import com.A21.Object.MSTSettings;
import com.A21.Object.MSTUser;
import com.A21.Object.TrnDetail;
import com.A21MobileSales.DataBase.DatabaseHelper;

public class PrintLayout {
	
	//Fongyi 
	public StringBuffer Layout1(Context context,String  TrnType ,String  CName ,String GSTInc,String GST,Boolean IsDO,MSTSettings setting,A21OrderHeader Header,ArrayList<TrnDetail> listDetail){
		StringBuffer sb = new StringBuffer();
		sb.append("{br}");
		sb.append("");
		if (!setting.PrintHeader1.equals("")) {
			sb.append("{reset}{center}"
					+ (setting.PrintHeader1IsBold ? "{b}" : "")
					+ setting.PrintHeader1 + "{br}");
		}
		if (!setting.PrintHeader2.equals("")) {
			sb.append("{reset}{center}"
					+ (setting.PrintHeader2IsBold ? "{b}" : "")
					+ setting.PrintHeader2 + "{br}");
		}
		if (!setting.PrintHeader3.equals("")) {
			sb.append("{reset}{center}"
					+ (setting.PrintHeader3IsBold ? "{b}" : "")
					+ setting.PrintHeader3 + "{br}");
		}
		if (!setting.PrintHeader4.equals("")) {
			sb.append("{reset}{center}"
					+ (setting.PrintHeader4IsBold ? "{b}" : "")
					+ setting.PrintHeader4 + "{br}");
		}
		if (!setting.PrintHeader5.equals("")) {
			sb.append("{reset}{center}"
					+ (setting.PrintHeader5IsBold ? "{b}" : "")
					+ setting.PrintHeader5 + "{br}");
		}
		if (!setting.PrintHeader6.equals("")) {
			sb.append("{reset}{center}"
					+ (setting.PrintHeader6IsBold ? "{b}" : "")
					+ setting.PrintHeader6 + "{br}");
		}
		if (!setting.PrintHeader7.equals("")) {
			sb.append("{reset}{center}"
					+ (setting.PrintHeader7IsBold ? "{b}" : "")
					+ setting.PrintHeader7 + "{br}");
		}
		
		String Line = String.format("%48s", "").replace(' ', '_');
		String DotLine = String.format("%48s", "").replace(' ', '-');
		SimpleDateFormat parsef = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		Date DateDisp = null;
		try {
			DateDisp = parsef.parse(Header.TrnDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		DateFormat dfDisp = new SimpleDateFormat("dd-MMM-yyyy hh:mm a");

		sb.append("{br}");
		if (TrnType.equals("CW")) {
			sb.append("{reset}ORDER REF DOC NO :" + Header.DocNo
					+ "{br}");
		} else if (TrnType.equals("CA")) {
			sb.append("{reset}CASH INVOICE DOC NO :" + Header.DocNo
					+ "{br}");
		} else if (TrnType.equals("CR")) {
			sb.append("{reset}CREDIT INVOICE DOC NO :" + Header.DocNo
					+ "{br}");
		}
		sb.append("{reset}" + Line + "{br}");
		sb.append("{reset}Date & Time  :" + dfDisp.format(DateDisp)
				+ "{br}");
		sb.append("{reset}Sales Person :" + Header.SalesPerson + "{br}");
		sb.append("{reset}Customer     :" + CName + "{br}");
		sb.append("{reset}Address      :" + Header.Add1 + "{br}");
		sb.append("{reset}" + Line + "{br}"); 
		int itemCnt = 0;
		DecimalFormat df2 = new DecimalFormat("#0.00");
		DecimalFormat df1 = new DecimalFormat("#");
		double SubTotal = 0;
		for (TrnDetail row : listDetail) {
			SubTotal += row.OrderQuantity
					* (row.UnitPrice - (row.UnitPrice * row.LineDiscount1));
			itemCnt += row.OrderQuantity;
			String code = "";
			String name = "";
			String qty = "";
			String up = "";
			String disc = "";
			String tot = "";  
			if(IsDO){ 
				if (row.Product.length() > 15) {
					code = row.Product.substring(0, 14);
				} else {
					code = String.format("%-15s", row.Product);
				}
				if (row.ProdDesc.length() > 23) {
					name = row.ProdDesc.substring(0, 22);
				} else {
					name = String.format("%-23s", row.ProdDesc);
				}
				qty = String.format("%10s", row.OrderQuantity);
				sb.append("{reset}" + code + name + qty + "{br}"); 
			}else{
				
				if (row.Product.length() > 15) {
					code = row.Product.substring(0, 14);
				} else {
					code = String.format("%-15s", row.Product);
				}
				if (row.ProdDesc.length() > 33) {
					name = row.ProdDesc.substring(0, 32);
				} else {
					name = String.format("%-33s", row.ProdDesc);
				}
				sb.append("{reset}" + code + name + "{br}");
				qty = String.format("%10s", row.OrderQuantity);
				double total = row.OrderQuantity
						* (row.UnitPrice - (row.UnitPrice * row.LineDiscount1));
				up = " X " + df2.format(row.UnitPrice);
				if (row.LineDiscount1 > 0) {
					disc = " Less : " + df2.format(row.LineDiscount1 * 100)
							+ "%";
				}
				up = String.format("%-25s", up + disc);
				tot = String.format("%13s", df2.format(total));
				sb.append("{reset}" + qty + up + tot + "{br}"); 
			}
			
		}
		sb.append("{reset}" + DotLine + "{br}");
		double GrandTotal = 0;
		double GSTAmount = 0;
		boolean GstInclusive = GSTInc.equals("1") ? true : false;
		double gstRate = Double.valueOf(GST);
		
		double DiscountPer=Header.Discount;
		double DiscountAmt=0; 
		DiscountAmt=Global.RD(Double.valueOf(SubTotal)*(DiscountPer), 2);  
		String strTotalLable="";
		if (GstInclusive) {
			strTotalLable="Total Less GST";
			GSTAmount =Global.RD((SubTotal-DiscountAmt)* (1 - (1 / (1 + gstRate))),2); 
			GrandTotal = SubTotal-DiscountAmt-GSTAmount;
		} else {
			strTotalLable="Total with GST";
			GSTAmount = (SubTotal-DiscountAmt) * gstRate; 
			GrandTotal = SubTotal-DiscountAmt+GSTAmount;
		}
		
		if(!IsDO){  
		sb.append("{reset}" + String.format("%33s", "Sub-Total :")
				+ String.format("%15s", df2.format(SubTotal))
				+ "{br}"); 
		sb.append("{reset}" + String.format("%33s", "Global Discount" + (Header.DiscountType.equals("P")? " ("+ (Global.RD( DiscountPer,2)* 100) +"%) :": " :"))
				+ String.format("%15s", df2.format(DiscountAmt))
				+ "{br}"); 
		sb.append("{reset}"
				+ String.format("%33s", df2.format(gstRate * 100) + "% GST" + " :")
				+ String.format("%15s", df2.format(GSTAmount)) + "{br}");
		sb.append("{reset}" + String.format("%33s", strTotalLable + " :")
				+ String.format("%15s", df2.format(GrandTotal)) + "{br}");
		sb.append("{reset}" + DotLine + "{br}");
		}
		 
		
		
		sb.append("{reset}{center}" + "ITEM COUNT : "
				+ df1.format(itemCnt) + "{br}");
		sb.append("{reset}" + DotLine + "{br}");
		sb.append("{br}");
		sb.append("{reset}{center}" + "RECEIVED BY" + "{br}");
		sb.append("{br}");
		sb.append("{br}");
		sb.append("{br}");
		sb.append("{br}");
		sb.append("{reset}" + Line + "{br}");
		sb.append("");
		if (!setting.PrintFooter1.equals("")) {
			sb.append("{reset}{center}"
					+ (setting.PrintFooter1IsBold ? "{b}" : "")
					+ setting.PrintFooter1 + "{br}");
		}
		if (!setting.PrintFooter2.equals("")) {
			sb.append("{reset}{center}"
					+ (setting.PrintFooter2IsBold ? "{b}" : "")
					+ setting.PrintFooter2 + "{br}");
		}
		if (!setting.PrintFooter3.equals("")) {
			sb.append("{reset}{center}"
					+ (setting.PrintFooter3IsBold ? "{b}" : "")
					+ setting.PrintFooter3 + "{br}");
		}
		if (!setting.PrintFooter4.equals("")) {
			sb.append("{reset}{center}"
					+ (setting.PrintFooter4IsBold ? "{b}" : "")
					+ setting.PrintFooter4 + "{br}");
		}
		if (!setting.PrintFooter5.equals("")) {
			sb.append("{reset}{center}"
					+ (setting.PrintFooter5IsBold ? "{b}" : "")
					+ setting.PrintFooter5 + "{br}");
		}
		if (!setting.PrintFooter6.equals("")) {
			sb.append("{reset}{center}"
					+ (setting.PrintFooter6IsBold ? "{b}" : "")
					+ setting.PrintFooter6 + "{br}");
		}
		if (!setting.PrintFooter7.equals("")) {
			sb.append("{reset}{center}"
					+ (setting.PrintFooter7IsBold ? "{b}" : "")
					+ setting.PrintFooter7 + "{br}");
		}
		return sb;
	}
 
	//Omni 
	public StringBuffer Layout2( Context context,String  TrnType ,String  CName ,String GSTInc,String GST,Boolean IsDO,MSTSettings setting,A21OrderHeader Header,ArrayList<TrnDetail> listDetail){
		StringBuffer sb = new StringBuffer();
		sb.append("{br}");
		sb.append("");
		if (!setting.PrintHeader1.equals("")) {
			sb.append("{reset}{center}"
					+ (setting.PrintHeader1IsBold ? "{b}" : "")
					+ setting.PrintHeader1 + "{br}");
		}
		if (!setting.PrintHeader2.equals("")) {
			sb.append("{reset}{center}"
					+ (setting.PrintHeader2IsBold ? "{b}" : "")
					+ setting.PrintHeader2 + "{br}");
		}
		if (!setting.PrintHeader3.equals("")) {
			sb.append("{reset}{center}"
					+ (setting.PrintHeader3IsBold ? "{b}" : "")
					+ setting.PrintHeader3 + "{br}");
		}
		if (!setting.PrintHeader4.equals("")) {
			sb.append("{reset}{center}"
					+ (setting.PrintHeader4IsBold ? "{b}" : "")
					+ setting.PrintHeader4 + "{br}");
		}
		if (!setting.PrintHeader5.equals("")) {
			sb.append("{reset}{center}"
					+ (setting.PrintHeader5IsBold ? "{b}" : "")
					+ setting.PrintHeader5 + "{br}");
		}
		if (!setting.PrintHeader6.equals("")) {
			sb.append("{reset}{center}"
					+ (setting.PrintHeader6IsBold ? "{b}" : "")
					+ setting.PrintHeader6 + "{br}");
		}
		if (!setting.PrintHeader7.equals("")) {
			sb.append("{reset}{center}"
					+ (setting.PrintHeader7IsBold ? "{b}" : "")
					+ setting.PrintHeader7 + "{br}");
		} 
		String Line = String.format("%48s", "").replace(' ', '_');
		String DotLine = String.format("%48s", "").replace(' ', '-');
		SimpleDateFormat parsef = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		Date DateDisp = null;
		try {
			DateDisp = parsef.parse(Header.TrnDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		DateFormat dfDisp = new SimpleDateFormat("dd-MMM-yyyy");

		sb.append("{br}");
		if (TrnType.equals("CW")) {
			sb.append("{reset}ORDER REF DOC NO :" + Header.DocNo
					+ "{br}");
		} else if (TrnType.equals("CA")) {
			sb.append("{reset}CASH INVOICE DOC NO :" + Header.DocNo
					+ "{br}");
		} else if (TrnType.equals("CR")) {
			sb.append("{reset}CREDIT INVOICE DOC NO :" + Header.DocNo
					+ "{br}");
		}
		sb.append("{reset}" + Line + "{br}");
		sb.append("{reset}Invoice Date  :" + dfDisp.format(DateDisp)
				+ "{br}");
		
		DatabaseHelper objDBHelper = new DatabaseHelper(context);
		MSTUser user=new MSTUser();
		try {
			user=objDBHelper.GetUserByUserId(Header.CreatedUser);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		sb.append("{reset}Sales Person  :" + user.EmployeeName + "{br}");
		sb.append("{reset}Customer Code :" + Header.Code + "{br}");
		sb.append("{reset}Customer      :" + CName + "{br}");
		sb.append("{reset}Address       :" + Header.Add1 + "{br}");
		sb.append("{reset}" + Line + "{br}"); 
		int itemCnt = 0;
		DecimalFormat df2 = new DecimalFormat("#0.00");
		DecimalFormat df1 = new DecimalFormat("#");
		DecimalFormat dfQty = new DecimalFormat("#0.000");
		double SubTotal = 0;
		for (TrnDetail row : listDetail) {
			SubTotal += row.OrderQuantity
					* (row.UnitPrice - (row.UnitPrice * row.LineDiscount1));
			itemCnt += row.OrderQuantity;
			String code = "";
			String name = "";
			String qty = "";
			String up = "";
			String disc = "";
			String tot = "";  
			if(IsDO){ 
				if (row.Product.length() > 15) {
					code = row.Product.substring(0, 14);
				} else {
					code = String.format("%-15s", row.Product);
				}
				if (row.ProdDesc.length() > 23) {
					name = row.ProdDesc.substring(0, 22);
				} else {
					name = String.format("%-18s", row.ProdDesc);
				}
				qty = String.format("%10s", dfQty.format( row.OrderQuantity))+  " " + String.format("%-4s", row.UOM);
				sb.append("{reset}" + code + name + qty + "{br}"); 
			}else{
				
				if (row.Product.length() > 15) {
					code = row.Product.substring(0, 14);
				} else {
					code = String.format("%-15s", row.Product);
				}
				if (row.ProdDesc.length() > 33) {
					name = row.ProdDesc.substring(0, 32);
				} else {
					name = String.format("%-33s", row.ProdDesc);
				}
				 
				sb.append("{reset}" + code + name + "{br}");
				qty = String.format("%10s", dfQty.format(row.OrderQuantity)) +  " "  + String.format("%-4s", row.UOM);
				double total = row.OrderQuantity
						* (row.UnitPrice - (row.UnitPrice * row.LineDiscount1));
				up = " X " + df2.format(row.UnitPrice);
				if (row.LineDiscount1 > 0) {
					disc = " Less : " + df2.format(row.LineDiscount1 * 100)
							+ "%";
				}
				up = String.format("%-20s", up + disc);
				tot = String.format("%13s", df2.format(total));
				sb.append("{reset}" + qty + up + tot + "{br}"); 
			}
			
		}
		sb.append("{reset}" + DotLine + "{br}");
		double GrandTotal = 0;
		double GSTAmount = 0;
		boolean GstInclusive = GSTInc.equals("1") ? true : false;
		double gstRate = Double.valueOf(GST);
		
		double DiscountPer=Header.Discount;
		double DiscountAmt=0; 
		DiscountAmt=Global.RD(Double.valueOf(SubTotal)*(DiscountPer), 2);  
		String strTotalLable="";
		if (GstInclusive) {
			strTotalLable="Total Less GST";
			GSTAmount =Global.RD((SubTotal-DiscountAmt)* (1 - (1 / (1 + gstRate))),2); 
			GrandTotal = SubTotal-DiscountAmt-GSTAmount;
		} else {
			strTotalLable="Total with GST";
			GSTAmount = (SubTotal-DiscountAmt) * gstRate; 
			GrandTotal = SubTotal-DiscountAmt+GSTAmount;
		}
		
		if(!IsDO){  
		sb.append("{reset}" + String.format("%33s", "Sub-Total :")
				+ String.format("%15s", df2.format(SubTotal))
				+ "{br}");  
		sb.append("{reset}"
				+ String.format("%33s", df2.format(gstRate * 100) + "% GST" + " :")
				+ String.format("%15s", df2.format(GSTAmount)) + "{br}");
		sb.append("{reset}" + String.format("%33s", strTotalLable + " :")
				+ String.format("%15s", df2.format(GrandTotal)) + "{br}");
		sb.append("{reset}" + DotLine + "{br}");
		}
		 
		 
		sb.append("{reset}{center}" + "ITEM COUNT : "
				+ df1.format(itemCnt) + "{br}");
		sb.append("{reset}" + DotLine + "{br}");
		sb.append("{br}");
		sb.append("{reset}{center}" + "RECEIVED BY" + "{br}");
		sb.append("{br}");
		sb.append("{br}");
		sb.append("{br}");
		sb.append("{br}");
		sb.append("{reset}" + Line + "{br}");
		sb.append("");
		if (!setting.PrintFooter1.equals("")) {
			sb.append("{reset}{center}"
					+ (setting.PrintFooter1IsBold ? "{b}" : "")
					+ setting.PrintFooter1 + "{br}");
		}
		if (!setting.PrintFooter2.equals("")) {
			sb.append("{reset}{center}"
					+ (setting.PrintFooter2IsBold ? "{b}" : "")
					+ setting.PrintFooter2 + "{br}");
		}
		if (!setting.PrintFooter3.equals("")) {
			sb.append("{reset}{center}"
					+ (setting.PrintFooter3IsBold ? "{b}" : "")
					+ setting.PrintFooter3 + "{br}");
		}
		if (!setting.PrintFooter4.equals("")) {
			sb.append("{reset}{center}"
					+ (setting.PrintFooter4IsBold ? "{b}" : "")
					+ setting.PrintFooter4 + "{br}");
		}
		if (!setting.PrintFooter5.equals("")) {
			sb.append("{reset}{center}"
					+ (setting.PrintFooter5IsBold ? "{b}" : "")
					+ setting.PrintFooter5 + "{br}");
		}
		if (!setting.PrintFooter6.equals("")) {
			sb.append("{reset}{center}"
					+ (setting.PrintFooter6IsBold ? "{b}" : "")
					+ setting.PrintFooter6 + "{br}");
		}
		if (!setting.PrintFooter7.equals("")) {
			sb.append("{reset}{center}"
					+ (setting.PrintFooter7IsBold ? "{b}" : "")
					+ setting.PrintFooter7 + "{br}");
		}
		return sb;
	}
	
}
